package sanglh.edu.hsphysics.main;

import java.io.File;
import java.util.ArrayList;

import sanglh.edu.hsphysics.CONFIG;
import sanglh.edu.hsphysics.CONST;
import sanglh.edu.hsphysics.R;
import sanglh.edu.hsphysics.base.BazeActionBarActivity;
import sanglh.edu.hsphysics.base.BazeItem;
import sanglh.edu.hsphysics.formulas.FormulaActivity;
import sanglh.edu.hsphysics.formulas.FormulaItem;
import sanglh.edu.hsphysics.main.FragmentNavigationDrawerItem.FormulaCallbacks;
import sanglh.edu.hsphysics.utils.AppUtils;
import sanglh.edu.hsphysics.utils.DeviceUtils;
import sanglh.edu.hsphysics.utils.FragmentUtil;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBar;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;

public class MenuActivity extends BazeActionBarActivity implements
		FragmentNavigationDrawer.NavigationDrawerCallbacks, FormulaCallbacks {

	/**
	 * Fragment managing the behaviors, interactions and presentation of the
	 * navigation drawer.
	 */
	private FragmentNavigationDrawer mNavigationDrawerFragment;

	/**
	 * Used to store the last screen title. For use in
	 * {@link #restoreActionBar()}.
	 */
	private CharSequence mTitle;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_menu);
		Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
		toolbar.setBackgroundColor(getResources().getColor(
				R.color.accent_material_light));
		setSupportActionBar(toolbar);

		mNavigationDrawerFragment = (FragmentNavigationDrawer) getSupportFragmentManager()
				.findFragmentById(R.id.navigation_drawer);
		mTitle = getTitle();

		// Set up the drawer.
		mNavigationDrawerFragment.setUp(R.id.navigation_drawer,
				(DrawerLayout) findViewById(R.id.drawer_layout), toolbar);
	}

	@Override
	public void onNavigationDrawerItemSelected(BazeItem item) {
		// update the main content by replacing fragments
		FragmentUtil.replaceTranslate(getSupportFragmentManager(),
				R.id.container, item.fragment, false);

	}

	public void onSectionAttached(String title) {
		mTitle = title;
	}

	@SuppressWarnings("deprecation")
	public void restoreActionBar() {
		ActionBar actionBar = getSupportActionBar();
		actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
		actionBar.setDisplayShowTitleEnabled(true);
		actionBar.setTitle(mTitle);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		if (!mNavigationDrawerFragment.isDrawerOpen()) {
			// Only show items in the action bar relevant to this screen
			// if the drawer is not showing. Otherwise, let the drawer
			// decide what to show in the action bar.
			// getMenuInflater().inflate(R.menu.menu, menu);
			restoreActionBar();
			return true;
		}
		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

	public void onSendMail(View viewF) {
		if (viewF != null) {
			CONFIG.deleteImageFolder(getApplicationContext());
		}
		String body = String
				.format("INFORMATION\nDevice : %s\nModel : %s\nManufacture : %s\nUUID : %s\n",
						Build.DEVICE, Build.MODEL, Build.MANUFACTURER,
						DeviceUtils.getUID(getApplicationContext()));
		File attactmentFolder = CONFIG.getImageFolder(getApplicationContext());
		File[] files = attactmentFolder.listFiles();
 		ArrayList<Uri> uris = null;
		if (files != null && files.length > 0) {
			uris = new ArrayList<Uri>();
			for (File file : files) {
				uris.add(Uri.fromFile(file));
			}
		}
		AppUtils.sendEmail(MenuActivity.this, CONST.REQUEST_CODE_SEND_EMAIL,
				"sanglehoang89@gmail.com",

				"[HSPhysic Support]", body, uris);
	}

	@Override
	public void onFormularSelected(FormulaItem item) {
		FormulaActivity.start(this, item.formulaResName, item.formulaResCount);
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);

		switch (requestCode) {
		case CONST.REQUEST_CODE_CAPTURE_IMAGE:
			if (resultCode == RESULT_OK) {
				onSendMail(null);
			}
			break;
		default:
			break;
		}

	}

}
